package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.core.enums.LendStatusEnum;
import com.atguigu.srb.core.pojo.entity.BorrowInfo;
import com.atguigu.srb.core.pojo.entity.Borrower;
import com.atguigu.srb.core.pojo.entity.Lend;
import com.atguigu.srb.core.mapper.LendMapper;
import com.atguigu.srb.core.pojo.vo.BorrowInfoApprovalVO;
import com.atguigu.srb.core.pojo.vo.BorrowerDetailVO;
import com.atguigu.srb.core.service.BorrowerService;
import com.atguigu.srb.core.service.DictService;
import com.atguigu.srb.core.service.LendService;
import com.atguigu.srb.core.util.LendNoUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 标的准备表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-09-13
 */
@Service
public class LendServiceImpl extends ServiceImpl<LendMapper, Lend> implements LendService {

    @Resource
    DictService dictService;
    @Resource
    BorrowerService borrowerService;
    @Override
    public void createLend(BorrowInfo borrowInfo, BorrowInfoApprovalVO borrowInfoApprovalVO) {
        Lend lend = new Lend();

        //生成标的单号
        String lendNo = LendNoUtils.getLendNo();
        lend.setLendNo(lendNo);
        //计算平台预期收益 = 月服务费率*借款总期数*借款总金额

        //月利率
        BigDecimal monthServiceRate = borrowInfoApprovalVO.getServiceRate().
                divide(new BigDecimal("100")).
                divide(new BigDecimal("12"), 2, BigDecimal.ROUND_HALF_DOWN);
        //预期收益
        BigDecimal exceptServiceAmount = monthServiceRate.
                multiply(borrowInfo.getAmount()).
                multiply(new BigDecimal(borrowInfo.getPeriod()));
        lend.setExpectAmount(exceptServiceAmount);

        //标的开始时间
        String lendStartDateStr = borrowInfoApprovalVO.getLendStartDate();
        LocalDate lendStartDate = LocalDate.parse(lendStartDateStr);
        lend.setLendStartDate(lendStartDate);
        //标的结束时间 = 开始时间+期数
        LocalDate lendEndDate = lendStartDate.plusMonths(borrowInfo.getPeriod());
        lend.setLendEndDate(lendEndDate);
        lend.setUserId(borrowInfo.getUserId()); //用户id
        lend.setAmount(borrowInfo.getAmount());
        lend.setPeriod(borrowInfo.getPeriod());
        lend.setBorrowInfoId(borrowInfo.getId());
        lend.setCheckAdminId(1l); //审核人id假的
        lend.setCheckTime(LocalDateTime.now());
        lend.setStatus(LendStatusEnum.INVEST_RUN.getStatus());
        lend.setTitle(borrowInfoApprovalVO.getTitle());
        lend.setLendYearRate(borrowInfoApprovalVO.getLendYearRate().
                divide(new BigDecimal("100")));
        lend.setServiceRate(borrowInfoApprovalVO.getServiceRate().
                divide(new BigDecimal("100")));
        lend.setReturnMethod(borrowInfo.getReturnMethod());
        lend.setLowestAmount(new BigDecimal(100));
        lend.setInvestAmount(new BigDecimal(0));
        lend.setInvestNum(0);
        lend.setPublishDate(LocalDateTime.now());
        lend.setLendInfo(borrowInfoApprovalVO.getLendInfo());
        lend.setRealAmount(new BigDecimal(0));

        baseMapper.insert(lend);


    }

    @Override
    public List<Lend> getList() {

        List<Lend> lends = baseMapper.selectList(null);
        /*流式编程赋值*/
        lends.stream().map((lend -> {
            lend.getParam().put("returnMethod",dictService.
                    getDictNameByDictCodeAndValueId("returnMethod",lend.getReturnMethod()));
            lend.getParam().put("status",LendStatusEnum.INVEST_RUN.getMsg());
            return lend;
        })).collect(Collectors.toList());
        return lends;

       /* *//*foreach 方式*//*
        lends.forEach(lend -> {
            String returnMethod = dictService.getDictNameByDictCodeAndValueId(
                    "returnMethod", lend.getReturnMethod());
            lend.getParam().put("returnMethod",returnMethod);
            String status = LendStatusEnum.INVEST_RUN.getMsg();
            lend.getParam().put("status",status);

        });
        return lends;*/
    }

    @Override
    public Map<String, Object> showLend(Long id) {
        //lend 标的详情
        Lend lend = baseMapper.selectById(id);
        lend.getParam().put("returnMethod",dictService.
                getDictNameByDictCodeAndValueId("returnMethod",lend.getReturnMethod()));
        lend.getParam().put("status",LendStatusEnum.INVEST_RUN.getMsg());
        Long userId = lend.getUserId();
        Map<String,Object> map = new HashMap<>();
        //borrower 借款人信息
        QueryWrapper<Borrower> borrowerQueryWrapper =new QueryWrapper<>();
        borrowerQueryWrapper.eq("user_id",userId);
        Borrower one = borrowerService.getOne(borrowerQueryWrapper);
        BorrowerDetailVO borrowerDetailVO = borrowerService.show(one.getId());
        map.put("borrower",borrowerDetailVO);
        map.put("lend",lend);


        return map;
    }

}
